/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pig.demo.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.security.annotation.Inner;
import com.pig4cloud.pig.demo.api.feign.RemoteDemoService;
import com.pig4cloud.pig.demo.entity.SysUser;
import com.pig4cloud.pig.demo.service.SysUserService;
import com.pig4cloud.plugin.idempotent.annotation.Idempotent;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import static com.pig4cloud.pig.common.core.constant.SecurityConstants.FROM_IN;


/**
 * 用户表
 *
 * @author Monkey
 * @date 2021-12-22 20:27:51
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/sysuser" )
@Api(value = "sysuser", tags = "用户表管理")
@Inner(value = false) //让外部系统能够直接调用
public class SysUserController {

    private final  SysUserService sysUserService;

	private final RemoteDemoService remoteDemoService;

	/**
	 * testFeign
	 * @return
	 */
	@ApiOperation(value = "testFeign", notes = "testFeign")
	@GetMapping("/testFeign" )
//	@PreAuthorize("@pms.hasPermission('demo_sysuser_get')" )
	public R testFeign() {
		com.pig4cloud.pig.demo.api.feign.SysLog sysLog = new com.pig4cloud.pig.demo.api.feign.SysLog();
		sysLog.setId(1l);
		return R.ok(remoteDemoService.saveLog(sysLog, FROM_IN));
	}


	@Inner(false)
	@Idempotent(key = "#key", expireTime = 10, info = "请勿重复查询(幂等操作，防止重复提交和调用)")
	@GetMapping("/test")
	public String test(String key) {
		return "success";
	}
    /**
     * 分页查询
     * @param page 分页对象
     * @param sysUser 用户表
     * @return
     */
//	@Idempotent(key = "#key", expireTime = 10, info = "请勿重复查询")
	@Idempotent( expireTime = 10, info = "请勿重复查询")
    @Inner(false)
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
//    @PreAuthorize("@pms.hasPermission('demo_sysuser_get')" )
    public R getSysUserPage(Page page, SysUser sysUser) {
        return R.ok(sysUserService.page(page, Wrappers.query(sysUser)));
    }


    /**
     * 通过id查询用户表
     * @param userId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{userId}" )
    @PreAuthorize("@pms.hasPermission('demo_sysuser_get')" )
    public R getById(@ApiParam(name = "这个也是用户id") @PathVariable("userId" ) Integer userId) {
        return R.ok(sysUserService.getById(userId));
    }

    /**
     * 新增用户表
     * @param sysUser 用户表
     * @return R
     */
    @ApiOperation(value = "新增用户表(已完成)", notes = "新增用户表(已完成)")
    @SysLog("新增用户表" )
    @PostMapping(value = "save")
    @PreAuthorize("@pms.hasPermission('demo_sysuser_add')" )
    public R save(@RequestBody SysUser sysUser) {
        return R.ok(sysUserService.save(sysUser));
    }

    /**
     * 修改用户表
     * @param sysUser 用户表
     * @return R
     */
    @ApiOperation(value = "修改用户表", notes = "修改用户表")
    @SysLog("修改用户表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('demo_sysuser_edit')" )
    public R updateById(@RequestBody SysUser sysUser) {
        return R.ok(sysUserService.updateById(sysUser));
    }

    /**
     * 通过id删除用户表
     * @param userId id
     * @return R
     */
    @ApiOperation(value = "通过id删除用户表（已完成）", notes = "通过id删除用户表（已完成）")
    @SysLog("通过id删除用户表" )
    @DeleteMapping("/{userId}" )
    @PreAuthorize("@pms.hasPermission('demo_sysuser_del')" )
    public R removeById(@ApiParam(name = "用户id") @PathVariable Integer userId) {
        return R.ok(sysUserService.removeById(userId));
    }

}
